﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.Workflows;
using Elsa.Workflows.Memory;
using Elsa.Workflows.Models;
using Newtonsoft.Json;
using T.FAS.Runtime.MF.Activities;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.SPI;
using ElsaExpression = Elsa.Expressions.Models.Expression;

namespace T.FAS.Runtime.MF.Activities.Nodes
{
    internal class LLMNode_back : NodeBase, INodeMapper
    {
        [JsonProperty("data")]
        public new LLMNodeData_back Data { get; set; }
        public List<Variable> Variables { get; set; } = new List<Variable>();
        public List<OutputDefinition> Outputs { get; set; }
        public List<InputDefinition> Inputs { get; set; } = new List<InputDefinition>();
        public IActivity MapToActivity()
        {
            var activity = new LLMActivity() { Id = Id, Name = Id };
            activity.LLMModel = Data.Model.Name;
            if (Data.Model.CompletionParams != null && Data.Model.CompletionParams.Count() > 0)
            {
                activity.LLMOptions = new List<BranchCondition>();
                foreach (var item in Data.Model.CompletionParams)
                {
                    activity.LLMOptions.Add(new BranchCondition { Label = item.Key, Condition = ElsaExpression.LiteralExpression(item.Value) });
                }
            }
            //上下文
            activity.Context = Data.Context.Enabled ? string.Join(".", Data.Context.VariableSelectors.Select(t => t)) : "";

            if (Data.PromptTemplate!=null && Data.PromptTemplate.Count()>0)
            {
                activity.Prompts = new List<BranchCondition>();
                foreach (var item in Data.PromptTemplate)
                {
                    activity.Prompts.Add(new BranchCondition { Label=$"{item.ID}/{item.Role}", Condition = ElsaExpression.LiteralExpression(item.Text) });
                }    
            }
            //失败策略
            activity.FaultedStrategy = "节点停止运行";

            return activity;
        }
    }
    internal class LLMNodeData_back
    {
        [JsonProperty("type")]
        public string Type { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("desc")]
        public string Desc { get; set; }
        [JsonProperty("variables")]
        public List<VariableNode_back> Variables { get; set; }
        [JsonProperty("prompt_template")]
        public List<PromptTemplateNode_back> PromptTemplate { get; set; }
        [JsonProperty("context")]
        public ContextNode_back Context { get; set; }
        [JsonProperty("vision")]
        public VersionNode_back Vision { get; set; }
        [JsonProperty("selected")]
        public bool Selected { get; set; }
        [JsonProperty("memory")]
        public MemoryNode_back Memory { get; set; }
        [JsonProperty("retry_config")]
        public RetryConfigNode_back RetryConfig { get; set; }
        [JsonProperty("model")]
        public ModelNode_back Model { get; set; }
    }
   internal class ContextNode_back
    {
        [JsonProperty("enabled")]
        public bool Enabled { get; set; }
        [JsonProperty("variable_selector")]
        public List<string> VariableSelectors { get; set; }
    }

    internal class MemoryNode_back
    {
        [JsonProperty("query_prompt_template")]
        public string QueryPromptTemplate { get; set; }
        [JsonProperty("role_prefix")]
        public Dictionary<string, string> RolePrefix { get; set; }

        [JsonProperty("window")]
        public WindowNode_back Window { get; set; }
    }

    internal class WindowNode_back
    {
        [JsonProperty("enabled")]
        public string Enabled { get; set; }
        [JsonProperty("size")]
        public int Size { get; set; }
    }

    internal class ModelNode_back
    {
        [JsonProperty("provider")]
        public string Provider { get; set; }
        [JsonProperty("Name")]
        public string Name { get; set; }
        [JsonProperty("Mode")]
        public string Mode { get; set; }
        [JsonProperty("completion_params")]
        public Dictionary<string,object> CompletionParams { get; set; }
    }
    internal class PromptTemplateNode_back
    {
        [JsonProperty("id")]
        public string ID { get; set; }
        [JsonProperty("role")]
        public string Role { get; set; }
        [JsonProperty("text")]
        public string Text { get; set; }
    }
    internal class RetryConfigNode_back
    {
        [JsonProperty("max_retries")]
        public int MaxRetries { get; set; }
        [JsonProperty("retry_enabled")]
        public bool RetryEnabled { get; set; }
        [JsonProperty("retry_interval")]
        public int RetryInterval { get; set; }
    }
    internal class VariableNode_back
    {
      
    }
    internal class VersionNode_back
    {
        [JsonProperty("enabled")]
        public bool Enabled { get; set; }
    }
}
